package stewardappanalysishandler

import (
	"github.com/gin-gonic/gin"
	"time"
	"yundian/internel/app/dao"
	"yundian/internel/app/helper"
	"yundian/internel/app/helper/request"
	"yundian/internel/app/models/statistics"
	"yundian/internel/pkg/dingo"
	. "yundian/internel/pkg/dingo/result"
	"yundian/internel/pkg/utils"
)

// HandleGetPlaceInfoData 门店分析-门店详情数据
func HandleGetPlaceInfoData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetAnalysisPlaceListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	if param.PlaceId <= 0 {
		Response(ctx)("", CodeInvalidParam, nil)(OK)
		return
	}

	info := dao.NewStatisticsDao().GetPlaceInfoDeviceData(param.PlaceId).Unwrap().(*request.GetAnalysisPlaceInfoDataRes)
	info.Date = time.Now().Format("2006-01-02 15:04:05")

	scan := dao.NewStatisticsDao().GetPlaceInfoScanNum(param.PlaceId).Unwrap().(*request.GetAnalysisPlaceInfoDataRes)
	info.ScanNum = scan.ScanNum

	oList := dao.NewStatisticsDao().GetPlaceOrderList([]int{param.PlaceId}, "").Unwrap().([]*request.GetAnalysisPlaceListRes)
	if len(oList) > 0 {
		info.OrderNum += oList[0].OrderNum
		info.SettlementCharge = oList[0].SettlementTotal
		info.SettlementTotal += oList[0].SettlementTotal
	}

	data := dao.NewStatisticsDao().GetPlaceInfoOrderDataList([]int{param.PlaceId}, "")
	moreList := data.Unwrap().([]*request.GetPlaceInfoOrderDataRes)
	for _, item := range moreList {
		if info.PlaceId == item.PlaceId {
			continue
		}
		info.OrderNum += item.OrderNum
		info.SettlementTotal += item.SettlementTotal
		if item.Type == 1 {
			info.SettlementAutomate = item.SettlementTotal
		}
		if item.Type == 2 {
			info.SettlementRoom = item.SettlementTotal
		}
		if item.Type == 3 {
			info.SettlementGoods = item.SettlementTotal
		}
	}

	Response(ctx)("", CodeSuccess, info)(OK)
}

// HandleGetPlaceList 门店分析-门店列表
func HandleGetPlaceList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetAnalysisPlaceListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetPlaceList(param)
	list := data.Unwrap().([]*request.GetAnalysisPlaceListRes)

	var placeIds []int
	for _, item := range list {
		placeIds = append(placeIds, item.PlaceId)
	}
	dList := dao.NewStatisticsDao().GetPlaceDeviceList(placeIds, param.Month).Unwrap().([]*request.GetAnalysisPlaceListRes)
	oList := dao.NewStatisticsDao().GetPlaceOrderList(placeIds, param.Month).Unwrap().([]*request.GetAnalysisPlaceListRes)
	moreList := dao.NewStatisticsDao().GetPlaceInfoOrderDataList([]int{param.PlaceId}, param.Month).Unwrap().([]*request.GetPlaceInfoOrderDataRes)
	for _, item := range list {
		for _, dItem := range dList {
			if item.PlaceId == dItem.PlaceId {
				item.DeviceNum = dItem.DeviceNum
				break
			}
		}
		for _, oItem := range oList {
			if item.PlaceId == oItem.PlaceId {
				item.SettlementTotal += oItem.SettlementTotal
				item.ActiveDeviceNum = oItem.UseDeviceNum
				item.UseDeviceNum = oItem.UseDeviceNum
				item.OrderNum += oItem.OrderNum
				break
			}
		}
		for _, mItem := range moreList {
			if item.PlaceId == mItem.PlaceId {
				item.SettlementTotal += mItem.SettlementTotal
				item.OrderNum += mItem.OrderNum
				break
			}
		}
	}

	Response(ctx)("", CodeSuccess, ResponseList{
		PageNo:     param.PageNo,
		PageSize:   param.PageSize,
		TotalCount: data.Val().(int64),
		TotalPage:  (data.Val().(int64) + int64(param.PageSize) - 1) / int64(param.PageSize),
		Data:       list,
	})(OK)
}

// HandleGetRoomIncomeMoneyList 查询续住收益-统计列表
func HandleGetRoomIncomeMoneyList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetRoomDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetRoomIncomeMoneyList(param).Unwrap().([]*statistics.GetRoomIncomeMoneyListRes)
	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetRoomOrderList 查询续住订单-统计列表
func HandleGetRoomOrderList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetRoomDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetRoomOrderList(param).Unwrap().([]*statistics.GetRoomOrderListRes)
	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetRoomOrderData 查询续住订单数据
func HandleGetRoomOrderData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetRoomDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetRoomOrderData(param).Unwrap().(*statistics.GetRoomOrderDataRes)
	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetRoomData 查询续住数据
func HandleGetRoomData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	param := &request.GetRoomDataListReq{UserId: userId}
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetRoomData(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetGoodsIncomeMoneyList 查询客房超市收益-统计列表
func HandleGetGoodsIncomeMoneyList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetGoodsDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetGoodsIncomeMoneyList(param).Unwrap().([]*statistics.GetGoodsIncomeMoneyListRes)
	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetGoodsOrderList 查询客房超市商品订单-统计列表
func HandleGetGoodsOrderList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetGoodsDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetGoodsOrderList(param).Unwrap().([]*statistics.GetGoodsOrderListRes)
	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetGoodsData 查询客房超市数据
func HandleGetGoodsData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	param := &request.GetGoodsDataListReq{UserId: userId}
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetGoodsData(param).Unwrap().(*statistics.GetGoodsDataRes)
	data.TotalIncome = data.SettlementTotal

	Response(ctx)("", CodeSuccess, data)(OK)
}

// HandleGetAutomateIncomeMoneyList 查询售货收益统计
func HandleGetAutomateIncomeMoneyList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetAutomateDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetAutomateIncomeMoneyList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetAutomateOrderGoodsList 查询售货商品订单统计列表
func HandleGetAutomateOrderGoodsList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetAutomateDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetAutomateOrderGoodsList(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetAutomateOrderGoodsData 查询售货商品订单数据
func HandleGetAutomateOrderGoodsData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetAutomateDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	data := dao.NewStatisticsDao().GetAutomateOrderGoodsData(param)
	Response(ctx)("", CodeSuccess, data.Unwrap())(OK)
}

// HandleGetAutomateData 查询售货数据
func HandleGetAutomateData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()
	param := &request.GetAutomateDataListReq{UserId: userId}
	param.IsAdmin, _ = helper.GetIsAdminFromCtx(ctx)
	param.ParentId, _ = helper.GetParentIdFromCtx(ctx)

	allData := dao.NewStatisticsDao().GetAutomateUserAnalysisAllData(userId).Unwrap().(*statistics.GetAutomateDataRes)
	LastData := dao.NewStatisticsDao().GetAutomateDevicesNumByUserId(param).Unwrap().(*statistics.GetAutomateDataRes)
	allData.TotalDevices = LastData.TotalDevices
	allData.TotalGoods = allData.TotalOrders

	Response(ctx)("", CodeSuccess, allData)(OK)
}

func HandleGetChargeUsePlaceData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	useData := dao.NewStatisticsDao().GetUserAnalysisUsePlaceData(param).Unwrap().(*statistics.GetChargeDataListRes)
	allData := dao.NewStatisticsDao().GetUserAnalysisLastDataByMonth(param).Unwrap().(*statistics.GetGetChargeDataRes)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"useNum": useData.Num,
		"allNum": utils.IfTrueInt(int64(allData.TotalPlace) >= useData.Num, allData.TotalPlace, int(useData.Num)),
	})(OK)
}

// HandleGetChargeUsePlaceNumList 查询充电门店动销占比
func HandleGetChargeUsePlaceNumList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	param.Fields = `total_place e_num,date_str date`
	listData := dao.NewStatisticsDao().GetUserAnalysisDataList(param).Unwrap().([]*statistics.GetChargeDataListRes)
	listUseData := dao.NewStatisticsDao().GetUserAnalysisUsePlaceDataDayList(param).Unwrap().([]*statistics.GetChargeDataListRes)
	for _, item := range listData {
		for _, uItem := range listUseData {
			if item.Date == uItem.Date {
				item.Num = uItem.Num
			}
		}
	}

	Response(ctx)("", CodeSuccess, listData)(OK)
}

func HandleGetChargeUseDeviceData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	useData := dao.NewStatisticsDao().GetUserAnalysisUseDeviceData(param).Unwrap().(*statistics.GetChargeDataListRes)
	allData := dao.NewStatisticsDao().GetUserAnalysisLastDataByMonth(param).Unwrap().(*statistics.GetGetChargeDataRes)
	Response(ctx)("", CodeSuccess, map[string]interface{}{
		"useNum": useData.Num,
		"allNum": utils.IfTrueInt(allData.TotalDevices >= useData.Num, int(allData.TotalDevices), int(useData.Num)),
	})(OK)
}

// HandleGetChargeUseDeviceNumList 查询充电设备动销占比
func HandleGetChargeUseDeviceNumList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	param.Fields = `total_devices e_num,date_str date`
	listData := dao.NewStatisticsDao().GetUserAnalysisDataList(param).Unwrap().([]*statistics.GetChargeDataListRes)
	listUseData := dao.NewStatisticsDao().GetUserAnalysisUseDeviceDataDayList(param).Unwrap().([]*statistics.GetChargeDataListRes)
	for _, item := range listData {
		for _, uItem := range listUseData {
			if item.Date == uItem.Date {
				item.Num = uItem.Num
			}
		}
	}

	Response(ctx)("", CodeSuccess, listData)(OK)
}

// HandleGetChargeOrderNumList 查询充电数据
func HandleGetChargeOrderNumList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	param.Fields = `total_orders num,settlement_total e_num,date_str date`
	listData := dao.NewStatisticsDao().GetUserAnalysisDataList(param)
	Response(ctx)("", CodeSuccess, listData.Unwrap())(OK)
}

// HandleGetChargeIncomeMoneyList 查询充电数据
func HandleGetChargeIncomeMoneyList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	param.Fields = `total_income num,date_str date`
	listData := dao.NewStatisticsDao().GetUserAnalysisDataList(param)
	Response(ctx)("", CodeSuccess, listData.Unwrap())(OK)
}

// HandleGetChargeSettlementMoneyList 查询充电数据
func HandleGetChargeSettlementMoneyList(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	param := &request.GetChargeDataListReq{}
	dingo.ErrorIf(ctx.ShouldBindJSON(param)).Unwrap()
	param.UserId = userId

	param.Fields = `settlement_total num,date_str date`
	listData := dao.NewStatisticsDao().GetUserAnalysisDataList(param)
	Response(ctx)("", CodeSuccess, listData.Unwrap())(OK)
}

// HandleGetChargeData 查询充电数据
func HandleGetChargeData(ctx *gin.Context) {
	userId, err := helper.GetUserIdFromCtx(ctx)
	dingo.ErrorIf(err).Unwrap()

	allData := dao.NewStatisticsDao().GetUserAnalysisAllData(userId).Unwrap().(*statistics.GetGetChargeDataRes)
	LastData := dao.NewStatisticsDao().GetUserAnalysisLastData(userId).Unwrap().(*statistics.GetGetChargeDataRes)
	allData.Date = LastData.Date
	allData.TotalPlace = LastData.TotalPlace
	allData.TotalDevices = LastData.TotalDevices

	Response(ctx)("", CodeSuccess, allData)(OK)
}
